#include "KohonenSynapse.h"

using namespace NeuralPlusPlus::Core;
using namespace NeuralPlusPlus::Core::SOM;

NeuralPlusPlus::Core::SOM::KohonenSynapse::KohonenSynapse( PositionNeuron *sourceNeuron, PositionNeuron *targetNeuron, KohonenConnector *parent )
	{
	Helper::ValidateNotNull(sourceNeuron, "sourceNeuron");
	Helper::ValidateNotNull(targetNeuron, "targetNeuron");
	Helper::ValidateNotNull(parent, "parent");

	this->Weight = 1;

	SourceNeuron->TargetSynapses.push_back(this);
	TargetNeuron->SourceSynapses.push_back(this);

	this->SourceNeuron = sourceNeuron;
	this->TargetNeuron = targetNeuron;
	this->Parent = parent;
	}

void NeuralPlusPlus::Core::SOM::KohonenSynapse::Propagate()
	{
	double similarity = SourceNeuron->Output - Weight;
	TargetNeuron->Input += similarity * similarity;
	}

void NeuralPlusPlus::Core::SOM::KohonenSynapse::OptimizeWeight( double learningFactor )
	{
	this->Weight += learningFactor * (SourceNeuron->Output - this->Weight);
	}

void NeuralPlusPlus::Core::SOM::KohonenSynapse::Jitter( double jitterNoiseLimit )
	{
	this->Weight += Helper::GetRandom(-jitterNoiseLimit, jitterNoiseLimit);
	}